package com.siwei.mes.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.entity.system.Role;
import com.siwei.mes.mapper.system.RoleMapper;
import com.siwei.mes.service.system.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 角色(Role)表服务实现类
 *
 * @author linzi
 * @since 2023-11-25 22:41:01
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public Long create(Role entity) {
        roleMapper.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void update(Role entity) {
        Role role = roleMapper.selectByPrimaryKey(entity.getId());
        assertTrue(role == null, "角色信息不存在");
        roleMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void delete(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        assertTrue(role == null, "角色信息不存在");
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Role> getPage(PageQuery<Role, Role> query) {
        Role searchParam = query.getParams();
        return PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> roleMapper.getList(searchParam));
    }

    @Override
    public Role loadById(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> getAll(Role entity) {
        return roleMapper.getList(entity);
    }
}
